import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import get from '@dfeidao/atom-web/local/get';

import set from '@dfeidao/atom-web/local/set';

import javadsd from '@dfeidao/atom-web/msg/javadsd';
import nodejs from '@dfeidao/atom-web/msg/nodejs';

import tree from '@dfeidao/fd-w000001';
interface B {
	docDataStore: {
		master: {
			dataArray: [{
				no: string;
				group_number: string
				group_name: string;
				formid: string;
				formname: string;
				grid_1: {
					dataArray: [
						{
							no: string;
							group_number: string;
							group_name: string;
							formid: string;
							formname: string;
						}
					]
				}
			}]
		};
	};
}
export default async function a004(fd: IFeidaoAiBrowserComponent, args: Event) {

	const name = args.currentTarget as HTMLDivElement;
	const name1 = fd.data.node.querySelectorAll<HTMLDivElement>('[xz]');
	set(fd, 'name', name.innerHTML);
	const tenantid = name.getAttribute('data-feidao-id');
	// debugger;
	const level = name.getAttribute('data-feidao-level');
	set(fd, 'tenantid', tenantid);
	set(fd, 'tenant_level', level);
	name1.forEach((i) => {
		i.classList.remove('w-bg-themef');
	});
	name.classList.add('w-bg-themef');
	const select1 = fd.data.node.querySelector<HTMLSelectElement>('[select]');
	const index = select1.options.selectedIndex;
	const client_type1 = select1.options[index].value;
	const condition = {
		param: {
			productid: tenantid,
			client_type: client_type1
		}
	};
	set(fd, 'client_type', client_type1);
	const ret = await javadsd('12', '12', 'smart_securitydsd0000010', condition) as B;
	const array = new Array();
	const array1 = new Array();

	ret.docDataStore.master.dataArray.forEach((i) => {
		i.group_number = i.group_number ? i.group_number : 'weifenzhu';
		if (i.grid_1.dataArray.length > 0) {
			i.grid_1.dataArray.forEach((j) => {
				j.no = i.group_number;
				array.push(j.formid);
				array1.push(j);
			});
		}
		// i.formid = i.group_number;
		i.formname = i.group_name;
		delete i.grid_1;
		i.no = '';
		// if (!i.formid) {
		// 	i.formid = JSON.stringify(idex);
		// }
		array1.push(i);
		array.push(i.formid);
	});

	const zds = fd.data.node.querySelector('[zds]') as tree;
	zds.uncheck_all();

	// console.log(array1, '000');
	zds.check_node(array);
	// debugger;
	const formid_arr = new Array();
	array1.forEach((v: { formid: string }) => {
		formid_arr.push(v.formid);
	});
	// console.log(formid_arr);
	const productid1 = get(fd, 'productid');

	const res1 = await nodejs<{ dataArray: Array<{ action_name: string, formid: string, action_keyword: string, code: string, _id: string, productid: string }> }>('tenant-function/zj-000001/s004', { filter: { productid: productid1, formid_arr } });//
	const a = new Map();
	// console.log(res1);
	res1.dataArray.forEach((i) => {
		if (Array.from(a.keys()).indexOf(i.formid) === -1) {
			a.set(i.formid, {
				left: {},
				right: [i]
			});
		} else {
			const te = a.get(i.formid);
			te.right.push(i);
		}
	});
	// console.log(array1);

	array1.forEach((item) => {
		if (item.group_name === "未分组") {
			item.group_name = '';
		}
		if (Array.from(a.keys()).indexOf(item.formid) === -1) {
			a.set(item.formid, {
				left: { client_type: item.client_type, create_time: item.create_time, formid: item.formid, formname: item.formname, group_number: item.group_number, group_name: item.group_name, package_no: item.package_no, package_name: item.package_name, package_path: item.package_path, productid: tenantid },
				right: []
			});
		} else {
			const te = a.get(item.formid);
			te.left = { client_type: item.client_type, create_time: item.create_time, formid: item.formid, formname: item.formname, group_number: item.group_number, group_name: item.group_name, package_no: item.package_no, package_name: item.package_name, package_path: item.package_path, productid: tenantid };
		}

	});
	// debugger;
	// console.log(a);
	set(fd, 'origin_obj', a);
	// const data = get(fd, 'origin_obj') as Map<string, { left: {}, right: Array<{}> }>;
	// console.log(a, 'a004');
}
